﻿/*******************************************************************************
 * Creator:admin 2022-05-07 19:30:40
 * Description: YoursLC有源低代码
*********************************************************************************/

using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using Think9.Models;
using Think9.Services.Base;
using Think9.Services.Basic;
using Think9.Services.Table;
using Think9.Services.Flow;
using Think9.Util.Helper;

namespace Think9.Services.CodeBuild
{
	/// <summary>
	/// 子表Service
	/// </summary>
	public class SubTable03Service : BaseService<SubTable03Model>
	{
		private ComService ComService = new ComService();
		private readonly string split = BaseUtil.ComSplit;//字符分割 用于多选项的分割等 如split+选项1+split+选项2+split
		private string sql;
		private string str;


		/// <summary>
		/// 返回子表数据
		/// </summary>
		/// <param name="listid">主表id</param>
		/// <param name="from">edit或add</param>
		/// <returns></returns>
		public List<SubTable03Model> GetGridDataByListId(CurrentUserEntity user, int listid, string from)
		{
			int count = 0;//行号
			DataTable dtindex = ComService.GetDataTable("select IndexId, BLock,BLock2,DataType,ControlType,DefaultV,RuleId,ListStat from tbindex where TbId = 'tb_SubTable03'  order by IndexNo");

			this.GetSelectList();

			List<SubTable03Model> list = new List<SubTable03Model>();
			//首行 待添加行 录入表基本属性可设置为可动态添加删除行
			SubTable03Model objAdd = new SubTable03Model();
			objAdd.ListId = listid;
			objAdd.Id = 0;//首行Id = 0
			objAdd.TbId = "tb_SubTable03";
			objAdd.flag = "#tb_SubTable03#0#0#";
			objAdd.v1_Exa = BasicHelp.GetDefaultValue(user, dtindex, "v1");
			objAdd.v1_Lock = BasicHelp.GetGridLockStr(dtindex, "v1", 0, from);
			objAdd.v2_Exa = BasicHelp.GetDefaultValue(user, dtindex, "v2");
			objAdd.v2_Lock = BasicHelp.GetGridLockStr(dtindex, "v2", 0, from);
			objAdd.v3_Exa = BasicHelp.GetDefaultValue(user, dtindex, "v3");
			objAdd.v3_Lock = BasicHelp.GetGridLockStr(dtindex, "v3", 0, from);
			objAdd.v4_JS ="  onclick = \" PopUpSelectValue('tb_SubTable03','v4','"+ objAdd.Id.ToString() +"') \"  ";
			objAdd.v4_Exa = BasicHelp.GetDefaultValue(user, dtindex, "v4");
			objAdd.v4_Lock = BasicHelp.GetGridLockStr(dtindex, "v4", 0, from);
			objAdd.v5_Exa = BasicHelp.GetDefaultValue(user, dtindex, "v5");
			objAdd.v5_Lock = BasicHelp.GetGridLockStr(dtindex, "v5", 0, from);
			objAdd.v6_Exa = BasicHelp.GetDefaultValue(user, dtindex, "v6");
			objAdd.v6_Lock = BasicHelp.GetGridLockStr(dtindex, "v6", 0, from);
			list.Add(objAdd);


			IEnumerable<SubTable03Model> _list = base.GetByWhere("where ListId=" + listid + "", null, null, "ORDER BY id");
			foreach (SubTable03Model obj in _list)
			{
				count++;//第几行 用于按行锁定等

				obj.TbId = "tb_SubTable03";
				obj.flag = "#tb_SubTable03#" + obj.Id.ToString() + "#" + count.ToString() + "#";
				//文本框日期
				obj.v1_Exa = obj.v1 == null ? "" : DateTime.Parse(obj.v1.ToString()).ToString("yyyy-MM-dd");
				obj.v1_Lock = BasicHelp.GetGridLockStr(dtindex, "v1", count, from);
				//文本框用户名
				obj.v2_Exa = obj.v2 == null ? "" : obj.v2.ToString();
				obj.v2_Lock = BasicHelp.GetGridLockStr(dtindex, "v2", count, from);
				//文本框默认值1
				obj.v3_Exa = obj.v3 == null ? "" : obj.v3.ToString();
				obj.v3_Lock = BasicHelp.GetGridLockStr(dtindex, "v3", count, from);
				//文本框默认值2
				obj.v4_JS ="  onclick = \" PopUpSelectValue('tb_SubTable03','v4','"+ obj.Id.ToString() +"') \"  ";
				obj.v4_Exa = obj.v4 == null ? "" : obj.v4.ToString();
				obj.v4_Lock = BasicHelp.GetGridLockStr(dtindex, "v4", count, from);
				//文本框默认值3
				obj.v5_Exa = obj.v5 == null ? "" : obj.v5.ToString();
				obj.v5_Lock = BasicHelp.GetGridLockStr(dtindex, "v5", count, from);
				//文本框无默认值
				obj.v6_Exa = obj.v6 == null ? "" : obj.v6.ToString();
				obj.v6_Lock = BasicHelp.GetGridLockStr(dtindex, "v6", count, from);
				list.Add(obj);
			}

			//合计行
			SubTable03Model sumobj = GetSumObj(listid, "tb_SubTable03", dtindex);
			if (sumobj.ListId != -1)
			{
				list.Add(sumobj);
			}

			return list;
		}
	   
	   /// <summary>
	   /// 将前台返回的json对象转化为通用子表对象，并进行格式及能否为空校验
	   /// </summary>
	   /// <param name="list">前台返回的子表数据列表</param>
	   /// <param name="flag">标志位，不为空时表示增加一条数据</param>
	   /// <param name="listid">主表id，当主表id=0时会先添加一条空数据返回自增长id</param>
	   /// <param name="strErr">错误信息</param>
	   /// <returns></returns>
	   public static List<SubTable03Model> GetGridFormList(IEnumerable<GridListEntity> list, string flag, int listid, ref string strErr)
		{
			string id = "";
			string row = "";
			string err = "";
			string tbid = "tb_SubTable03";
			string temp = "";
			bool badd = false;

			List<SubTable03Model> mlist = new List<SubTable03Model>();

			foreach (GridListEntity obj in list)
			{
				badd = false;
				if (!string.IsNullOrEmpty(flag))//添加一条数据
				{
					if (obj.flag.StartsWith(flag))
					{
						badd = true;
					}
				}
				else//编辑列表数据
				{
                    //排除首行即增加行
                    if (obj.flag.StartsWith("#" + tbid + "#") && !obj.flag.EndsWith("#0#0#"))
                    {
                        badd = true;
                    }
				}

				if (badd)
				{
					err = "";
					BasicHelp.GetRowAndIdByFlag(obj.flag, ref id, ref row);

					temp = obj.v1 == null ? "" : obj.v1;
					if (!ValidatorHelper.IsDateTime(temp) && !string.IsNullOrEmpty(temp))
					{
						err += row + "日期日期格式错误";
					}
					temp = obj.v3 == null ? "" : obj.v3;
					if (!ValidatorHelper.IsInteger(temp) && !string.IsNullOrEmpty(temp))
					{
						err += row + "默认值1不是整数";
					}

					if (string.IsNullOrEmpty(err))
					{
						SubTable03Model model = new SubTable03Model();
						model.TbId = tbid;
						model.ListId = listid;
						model.Id = int.Parse(id);
						model.v1 = obj.v1 == null ? null : DateTime.Parse(obj.v1);
						model.v2 = obj.v2 == null ? "" : obj.v2;
						model.v3 = string.IsNullOrEmpty(obj.v3) ? null : int.Parse(obj.v3);
						model.v4 = obj.v4 == null ? "" : obj.v4;
						model.v5 = obj.v5 == null ? "" : obj.v5;
						model.v6 = obj.v6 == null ? "" : obj.v6;

						mlist.Add(model);
					}
					else
					{
						strErr += "子表03 " + err;
					}
				}
			}

			return mlist;
		}



		//子表 为下拉选择、多选、单选动态数据源 来源于为指标指定的数据规范
		public IEnumerable<valueTextEntity> GetSelectList()
		{
			DataTable dt = DataTableHelp.NewValueTextDt();


			//DataTable转换成IEnumerable
			return DataTableHelp.ToEnumerable<valueTextEntity>(dt);
		}

		//合计行
		private SubTable03Model GetSumObj(int listid, string tbid, DataTable dtindex)
		{
			TbIndexService TbIndexService = new TbIndexService();

			SubTable03Model obj = new SubTable03Model();
			obj.ListId = -1;

			var _index = DataTableHelp.ToEnumerable<TbIndexEntity>(dtindex);
			if (_index.Where(x => x.ControlType == "1" && x.ListStat == "1" && x.DataType.StartsWith("3")).Count<TbIndexEntity>() > 0)//条件 1数值指标2文本框3统计求和
			{
				obj.ListId = listid;
				obj.Id = -1;
				obj.TbId = tbid;
				obj.flag = "";
				foreach (TbIndexEntity item in _index)
				{
				    //1数值指标2文本框3统计求和
					if (item.ControlType == "1" && item.ListStat == "1" && item.DataType.StartsWith("3"))
					{
						switch (item.IndexId)
						{
							case "v1":
								obj.v1_Exa = "∑ = " + ComService.GetSingle("select SUM(" + item.IndexId + ") from " + obj.TbId + " where ListId = " + listid + "");
								 obj.v1_Lock = "disabled = 'disabled'";
								break;
							case "v2":
								obj.v2_Exa = "∑ = " + ComService.GetSingle("select SUM(" + item.IndexId + ") from " + obj.TbId + " where ListId = " + listid + "");
								 obj.v2_Lock = "disabled = 'disabled'";
								break;
							case "v3":
								obj.v3_Exa = "∑ = " + ComService.GetSingle("select SUM(" + item.IndexId + ") from " + obj.TbId + " where ListId = " + listid + "");
								 obj.v3_Lock = "disabled = 'disabled'";
								break;
							case "v4":
								obj.v4_Exa = "∑ = " + ComService.GetSingle("select SUM(" + item.IndexId + ") from " + obj.TbId + " where ListId = " + listid + "");
								 obj.v4_Lock = "disabled = 'disabled'";
								break;
							case "v5":
								obj.v5_Exa = "∑ = " + ComService.GetSingle("select SUM(" + item.IndexId + ") from " + obj.TbId + " where ListId = " + listid + "");
								 obj.v5_Lock = "disabled = 'disabled'";
								break;
							case "v6":
								obj.v6_Exa = "∑ = " + ComService.GetSingle("select SUM(" + item.IndexId + ") from " + obj.TbId + " where ListId = " + listid + "");
								 obj.v6_Lock = "disabled = 'disabled'";
								break;

							default:
								break;
						}
					}
					else
					{
						switch (item.IndexId)
						{
							case "v1":
								obj.v1_Exa = "";
								 obj.v1_Lock = "disabled = 'disabled'";
								break;
							case "v2":
								obj.v2_Exa = "";
								 obj.v2_Lock = "disabled = 'disabled'";
								break;
							case "v3":
								obj.v3_Exa = "";
								 obj.v3_Lock = "disabled = 'disabled'";
								break;
							case "v4":
								obj.v4_Exa = "";
								 obj.v4_Lock = "disabled = 'disabled'";
								break;
							case "v5":
								obj.v5_Exa = "";
								 obj.v5_Lock = "disabled = 'disabled'";
								break;
							case "v6":
								obj.v6_Exa = "";
								 obj.v6_Lock = "disabled = 'disabled'";
								break;

							default:
								break;
						}
					}
				}
			}

			return obj;
		}


	}
}